package com.zx.sp_mybatisplus_smbms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zx.sp_mybatisplus_smbms.pojo.SmbmsHero;
import com.zx.sp_mybatisplus_smbms.service.SmbmsHeroService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/heroController/")
public class HeroController {
    @Autowired
    private SmbmsHeroService smbmsHeroService;

    @RequestMapping("/")
    public String helloHero(Model model){
        return findHerosByParam(model,1,null,null);
    }

    @RequestMapping("findAllHero/{current}")
    public String findAllHero(Model model,@PathVariable("current") Integer current){
        if (current == null){
            current = 1;
        }
        Page<SmbmsHero> page = new Page<>(current,6);
        smbmsHeroService.page(page);
        //List<SmbmsHero> list = smbmsHeroService.list();
        model.addAttribute("page",page);
        model.addAttribute("herolist",page.getRecords());
        return "herolist";
    }

    @RequestMapping("findHeroById")
    public String findHeroById(Model model,Integer id,String method){
        List<Integer> list = new ArrayList();
        list.add(id);
        List<SmbmsHero> smbmsHeroes = smbmsHeroService.listByIds(list);
        model.addAttribute("hero",smbmsHeroes.get(0));
        if (method.equals("viewHero")){
            return "heroview";
        } else {
            return "heromodify";
        }

    }

    @RequestMapping("delHeroById")
    public String delHeroById(Model model,Integer id,String method){
        List<Integer> list = new ArrayList();
        list.add(id);
        boolean b = smbmsHeroService.removeByIds(list);
        if (b){
            return "forward:findAllHero";
        } else {
            return "forward:findAllHero";
        }

    }


    @RequestMapping("findHerosByParam")
    public String findHerosByParam(Model model,Integer current ,String queryname,String querycname){
        QueryWrapper<SmbmsHero> wrapper = new QueryWrapper<SmbmsHero>();
        if (queryname!=null){
            wrapper.like("name",queryname);
        }
        if (querycname!=null){
            wrapper.like("nickname",querycname);
        }
        if (current<=0){
            current =  1;
        }
        List<SmbmsHero> list = smbmsHeroService.list(wrapper);
        Integer i;
        if (list.size()%7==0){
            i=list.size()/7;
        }else{
            i=list.size()/7 + 1;
        }
        Page<SmbmsHero> page = null;
        if (i < current){
            page = new Page<>(i,7);
            smbmsHeroService.page(page,wrapper);
        } else {
            page = new Page<>(current,7);
            smbmsHeroService.page(page,wrapper);
        }
        model.addAttribute("page",page);
        model.addAttribute("herolist",page.getRecords());
        model.addAttribute("queryname",queryname);
        model.addAttribute("querycname",querycname);

        return "herolist";

    }

    @RequestMapping("addHero")
    public String addHero(Model model,SmbmsHero smbmsHero){
        boolean save = smbmsHeroService.save(smbmsHero);
        if (save) {
            return "forward:findAllHero";
        } else {
            return "forward:findAllHero";
        }

    }

    @RequestMapping("updateHero")
    public String updateHero(Model model,SmbmsHero smbmsHero){
        boolean save = smbmsHeroService.updateById(smbmsHero);
        if (save) {
            return "forward:findAllHero";
        } else {
            return "forward:findAllHero";
        }

    }



    @RequestMapping("html_add")
    public String html_add(Model model){
        return "heroadd";
    }
}
